package limeirafol.gui.administracao;

import lbframework.bean.internalFrame.LBInternalFrame;
import lbframework.bean.mdiFrame.LBMdiFrame;
import lbframework.classe.Util;
import limeirafol.bean.BeanAdministacao;
import limeirafol.gui.Cadastro.ProdutoConsultaGUI;
import limeirafol.gui.MenuGUI;
import limeirafol.remote.administracao.AdministracaoPrecoVO;
import limeirafol.remote.cadastro.Situacao;

public class AdministracaoPrecoGUI extends LBInternalFrame {

    AdministracaoPrecoVO oAdministracao = new AdministracaoPrecoVO();
    BeanAdministacao beanAdministracao = new BeanAdministacao();

    public AdministracaoPrecoGUI(LBMdiFrame i_mdiFrame) throws Exception {
        super(i_mdiFrame);

        centralizarForm();

        initComponents();

        cnsProduto.setTabela("produto");
        cnsProduto.setFiltro("AND id_situacaocadastro = " + Situacao.ATIVO.getId());
    }

    public void carregarAdministracao(int i_id) {
        int i = 0;
    }

    public void carregarCusto() throws Exception {
        oAdministracao = beanAdministracao.carregarCusto(cnsProduto.getId());

        txtCusto.setDouble(oAdministracao.custoAtual);
        txtPorcentagem.setDouble(oAdministracao.porcentagem);
        txtPrecoVenda.setDouble(oAdministracao.precoVendaAtual);

        getEstadoTelaInicial();

    }

    @Override
    public void salvar() throws Exception {
        oAdministracao.idProduto = cnsProduto.getId();
        oAdministracao.custoAtual = txtCusto.getDouble();
        oAdministracao.porcentagem = txtPorcentagem.getDouble();
        oAdministracao.precoVendaAtual = txtPrecoVenda.getDouble();

        beanAdministracao.salvarAdministracaoPreco(oAdministracao);

        Util.exibirMensagem(Util.MSG_SALVO_SUCESSO, getTitle());

        getEstadoTelaInicial();
    }

    private void calculaPrecoVenda() throws Exception {
        double precoVenda = 0;
        double custoAtual = txtCusto.getDouble();
        double porcentagem = txtPorcentagem.getDouble();

        precoVenda = ((custoAtual * (porcentagem / 100)) + custoAtual);

        txtPrecoVenda.setDouble(precoVenda);

    }

    private void calculaPorcentagem() throws Exception {
        double precoVenda = txtPrecoVenda.getDouble();
        double custoAtual = txtCusto.getDouble();
        double porcentagem = 0;

        porcentagem = ((precoVenda - custoAtual) / custoAtual) * 100;

        txtPorcentagem.setDouble(porcentagem);

    }

    public void setCampoId(int i_id) throws Exception {
        cnsProduto.setId(i_id);
        cnsProduto.consultar();
        cnsProduto.setEnabled(false);

        calculaPrecoVenda();
    }

    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lBButtonGroup1 = new lbframework.bean.buttonGroup.LBButtonGroup();
        lBToolBarPadrao1 = new lbframework.bean.toolBarPadrao.LBToolBarPadrao();
        lBPanel1 = new lbframework.bean.panel.LBPanel();
        cnsProduto = new lbframework.bean.consultaCampo.LBConsultaCampo();
        lBLabel1 = new lbframework.bean.label.LBLabel();
        lBLabel2 = new lbframework.bean.label.LBLabel();
        txtPrecoVenda = new lbframework.bean.textField.LBTextField();
        txtPorcentagem = new lbframework.bean.textField.LBTextField();
        lBLabel3 = new lbframework.bean.label.LBLabel();
        lBLabel4 = new lbframework.bean.label.LBLabel();
        txtCusto = new lbframework.bean.textField.LBTextField();
        lBPanel2 = new lbframework.bean.panel.LBPanel();
        lBButton1 = new lbframework.bean.button.LBButton();
        lBButton2 = new lbframework.bean.button.LBButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("Administração de Preço");
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        lBToolBarPadrao1.setRollover(true);
        lBToolBarPadrao1.setVisibleSalvar(true);

        cnsProduto.setObrigatorio(true);
        cnsProduto.addEventoConsultaCampoListener(new lbframework.bean.consultaCampo.LBEventoConsultaCampoListener() {
            public void abreTelaConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {
                cnsProdutoAbreTelaConsulta(evt);
            }
            public void aposConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {
                cnsProdutoAposConsulta(evt);
            }
        });

        lBLabel1.setText("Produto:");

        lBLabel2.setText("Preço Venda");

        txtPrecoVenda.setMascara("Decimal 2");
        txtPrecoVenda.setObrigatorio(true);
        txtPrecoVenda.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPrecoVendaFocusLost(evt);
            }
        });

        txtPorcentagem.setMascara("Decimal 2");
        txtPorcentagem.setObrigatorio(true);
        txtPorcentagem.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPorcentagemFocusLost(evt);
            }
        });

        lBLabel3.setText("Porcentagem");

        lBLabel4.setText("Preço Custo");

        txtCusto.setEnabled(false);
        txtCusto.setMascara("Decimal 2");

        javax.swing.GroupLayout lBPanel1Layout = new javax.swing.GroupLayout(lBPanel1);
        lBPanel1.setLayout(lBPanel1Layout);
        lBPanel1Layout.setHorizontalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(cnsProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lBLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtCusto, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPorcentagem, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(txtPrecoVenda, javax.swing.GroupLayout.PREFERRED_SIZE, 80, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        lBPanel1Layout.setVerticalGroup(
            lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lBLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(cnsProduto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(lBPanel1Layout.createSequentialGroup()
                        .addComponent(lBLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(txtCusto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(lBPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addGroup(lBPanel1Layout.createSequentialGroup()
                            .addComponent(lBLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtPorcentagem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGroup(lBPanel1Layout.createSequentialGroup()
                            .addComponent(lBLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(txtPrecoVenda, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        lBButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/sair.png"))); // NOI18N
        lBButton1.setText("Sair");
        lBButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lBButton1ActionPerformed(evt);
            }
        });

        lBButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/lbframework/img/salvar.png"))); // NOI18N
        lBButton2.setText("Salvar");
        lBButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                lBButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout lBPanel2Layout = new javax.swing.GroupLayout(lBPanel2);
        lBPanel2.setLayout(lBPanel2Layout);
        lBPanel2Layout.setHorizontalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, lBPanel2Layout.createSequentialGroup()
                .addContainerGap(227, Short.MAX_VALUE)
                .addComponent(lBButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );
        lBPanel2Layout.setVerticalGroup(
            lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(lBPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                .addComponent(lBButton1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addComponent(lBButton2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addComponent(lBToolBarPadrao1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 100, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lBPanel1, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(lBPanel2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(lBToolBarPadrao1, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lBPanel2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(23, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void lBButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lBButton1ActionPerformed
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_lBButton1ActionPerformed

    private void lBButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_lBButton2ActionPerformed
        try {
            salvar();

        } catch (Exception e) {
            Util.exibirMensagemErro(e, getTitle());
        }
    }//GEN-LAST:event_lBButton2ActionPerformed

    private void cnsProdutoAbreTelaConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {//GEN-FIRST:event_cnsProdutoAbreTelaConsulta
        try {
            MenuGUI menu = (MenuGUI) mdiFrame;

            if (menu.formProdutoConsulta == null || menu.formProdutoConsulta.isClosed()) {
                menu.formProdutoConsulta = new ProdutoConsultaGUI(mdiFrame);
            }

            menu.formProdutoConsulta.setConsultaCampo(cnsProduto);
            menu.formProdutoConsulta.setVisible(true);

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }

    }//GEN-LAST:event_cnsProdutoAbreTelaConsulta

    private void cnsProdutoAposConsulta(lbframework.bean.consultaCampo.LBEventoConsultaCampo evt) {//GEN-FIRST:event_cnsProdutoAposConsulta
        try {
            carregarCusto();

        } catch (Exception e) {
            Util.exibirMensagemErro(e, getTitle());
        }

    }//GEN-LAST:event_cnsProdutoAposConsulta

    private void txtPorcentagemFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPorcentagemFocusLost
        try {
            calculaPrecoVenda();

        } catch (Exception e) {
            Util.exibirMensagemErro(e, getTitle());
        }
    }//GEN-LAST:event_txtPorcentagemFocusLost

    private void txtPrecoVendaFocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPrecoVendaFocusLost
        try {
            calculaPorcentagem();

        } catch (Exception e) {
            Util.exibirMensagemErro(e, getTitle());
        }
    }//GEN-LAST:event_txtPrecoVendaFocusLost

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        try {
            verificarAlteracaoTela();
            sair();

        } catch (Exception ex) {
            Util.exibirMensagemErro(ex, getTitle());
        }
    }//GEN-LAST:event_formInternalFrameClosing
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private lbframework.bean.consultaCampo.LBConsultaCampo cnsProduto;
    private lbframework.bean.button.LBButton lBButton1;
    private lbframework.bean.button.LBButton lBButton2;
    private lbframework.bean.buttonGroup.LBButtonGroup lBButtonGroup1;
    private lbframework.bean.label.LBLabel lBLabel1;
    private lbframework.bean.label.LBLabel lBLabel2;
    private lbframework.bean.label.LBLabel lBLabel3;
    private lbframework.bean.label.LBLabel lBLabel4;
    private lbframework.bean.panel.LBPanel lBPanel1;
    private lbframework.bean.panel.LBPanel lBPanel2;
    private lbframework.bean.toolBarPadrao.LBToolBarPadrao lBToolBarPadrao1;
    private lbframework.bean.textField.LBTextField txtCusto;
    private lbframework.bean.textField.LBTextField txtPorcentagem;
    private lbframework.bean.textField.LBTextField txtPrecoVenda;
    // End of variables declaration//GEN-END:variables
}
